/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.core.Cloneable;
import fever.structures.IColor;
import fever.utils.ColorSpace;
import fever.utils.Stringifier;

/**
 * Defines color using RGB componants.
 * 
 * @see fever.utils.ColorSpace
 * 
 * @author Romain Ecarnot
 */
class fever.structures.color.RGB implements IColor, Cloneable
{
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private var _red : Number;
	private var _green : Number;
	private var _blue : Number;
	private var _hexa : Number;
	private var _string : String;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Red value. */
	public function set red( v : Number ) : Void { _setRed( v ); }		public function get red() : Number { return _red; }	
	
	/** Green value. */
	public function set green( v : Number ) : Void { _setGreen( v ); }	
	public function get green() : Number { return _green; }
	
	/** Blue value. */
	public function set blue( v : Number ) : Void { _setBlue( v ); }	
	public function get blue() : Number { return _blue; }
		
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor..
	 */
	public function RGB( r : Number, g : Number, b : Number )
	{
		_setRed( r );		_setGreen( g );		_setBlue( b );
	}
	
	/** 
	 * Returns color value in Number format.
	 */
	public function getColor() : Number
	{
		return ColorSpace.rgbToHexa( this );
	}
	
	/**
	 * Return color value in hexa String format.
	 */ 
	public function getString() : String
	{
		return ColorSpace.rgbToString( this );
	}
	
	/**
	 * Returns a clone instance.
	 * 
	 * @return Cloneable
	 */
	public function clone() : Cloneable
	{
		return new RGB( _red, _green, _blue );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return 	Stringifier.parse( this, "RGB [" + _red + "." + _green + "." + _blue + "]" );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _setRed( value : Number ) : Void
	{
		if( value >= 0 && value <= 255 ) _red = value;
		else _red = 0;	
	}
	
	private function _setGreen( value : Number ) : Void
	{
		if( value >= 0 && value <= 255 ) _green = value;
		else _green = 0;	
	}
	
	private function _setBlue( value : Number ) : Void
	{
		if( value >= 0 && value <= 255 ) _blue = value;
		else _blue = 0;	
	}
}